package com.luobd.bpm.activiti;

import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManagerImpl;
import org.activiti.engine.impl.persistence.entity.data.ExecutionDataManager;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.IdentityLinkType;

public class CustomExecutionEntityManagerImpl extends ExecutionEntityManagerImpl implements CustomExecutionEntityManager{
    public CustomExecutionEntityManagerImpl(ProcessEngineConfigurationImpl processEngineConfiguration, ExecutionDataManager executionDataManager) {
        super(processEngineConfiguration, executionDataManager);
    }

    @Override
    public ExecutionEntity createProcessInstanceExecution(ProcessDefinition processDefinition, String processInstanceId, String businessKey, String tenantId, String initiatorVariableName) {
//创建流程实例
        ExecutionEntity processInstanceExecution = executionDataManager.create();
        //初始化流程实例的属性
        processInstanceExecution.setId(processInstanceId);
        processInstanceExecution.setProcessDefinitionId(processDefinition.getId());
        processInstanceExecution.setProcessDefinitionKey(processDefinition.getKey());
        processInstanceExecution.setProcessDefinitionName(processDefinition.getName());
        processInstanceExecution.setProcessDefinitionVersion(processDefinition.getVersion());
        processInstanceExecution.setBusinessKey(businessKey);
        processInstanceExecution.setScope(true);
        if (tenantId != null) {
            processInstanceExecution.setTenantId(tenantId);
        }
        String authenticatedUserId = Authentication.getAuthenticatedUserId();
        processInstanceExecution.setStartTime(Context.getProcessEngineConfiguration().
                getClock().getCurrentTime());
        processInstanceExecution.setStartUserId(authenticatedUserId);
        //保存流程实例
        insert(processInstanceExecution, false);
        if (initiatorVariableName != null) {
            processInstanceExecution.setVariable(initiatorVariableName, authenticatedUserId);
        }
        processInstanceExecution.setProcessInstanceId(processInstanceExecution.getId());
        processInstanceExecution.setRootProcessInstanceId(processInstanceExecution
                .getId());
        if (authenticatedUserId != null) {
            getIdentityLinkEntityManager().addIdentityLink(processInstanceExecution,
                    authenticatedUserId, null, IdentityLinkType.STARTER);
        }
        //发送事件
        if (getEventDispatcher().isEnabled()) {
            getEventDispatcher().dispatchEvent(ActivitiEventBuilder
                    .createEntityEvent(ActivitiEventType.ENTITY_CREATED, processInstanceExecution));
        }
        return processInstanceExecution;
    }
}
